package com.vitabuybuy.tv.ui.home.group

import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import com.library.common.base.BaseActivity
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivitySearchGroupbuyOrBiddingOrLiveBinding
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.home.bidd.SearchBiddingResultActivity
import com.vitabuybuy.tv.ui.home.group.viewmodel.GroupBuyZoneViewModel
import com.vitabuybuy.tv.ui.home.live.SearchLiveResultActivity
import com.vitabuybuy.tv.utils.KeyBoardUtils
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import kotlinx.android.synthetic.main.activity_search_groupbuy_or_bidding_or_live.*
import java.util.*

/**
 * 作者：SimGa
 * 日期：2021/1/7
 * 时间：11:01
 * 包名：com.vitabuybuy.tv.ui.home.group
 */
class SearchGroupBuyOrBiddingOrLiveActivity :
    BaseActivity<GroupBuyZoneViewModel, ActivitySearchGroupbuyOrBiddingOrLiveBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = false
    override val layoutId: Int
        get() = R.layout.activity_search_groupbuy_or_bidding_or_live

    val tiplist = ArrayList<String>()

    private var from = FROM_GROUP

    override fun onGetBundle(bundle: Bundle?) {
        super.onGetBundle(bundle)
        from = bundle!!.getInt("from")
    }

    override fun initView(savedInstanceState: Bundle?) {
        tagLayout!!.adapter = object : TagAdapter<String>(tiplist) {
            override fun getView(parent: FlowLayout, position: Int, str: String): View {
                val tvTip = LayoutInflater.from(mContext).inflate(
                    R.layout.item_tag,
                    tagLayout, false
                ) as TextView
                tvTip.text = str
                val ll = tvTip.layoutParams as ViewGroup.MarginLayoutParams
                ll.rightMargin = 50
                ll.bottomMargin = 40
                tvTip.layoutParams = ll
                return tvTip
            }
        }
    }

    override fun observeData() {
        viewModel.gethotKey()
        viewModel.hotKeylist.observe(this, Observer {
            for (str in it) {
                tiplist.add(str.name)
            }
            tagLayout.adapter.notifyDataChanged()
        })
    }

    override fun eventHandling() {
        tagLayout.setOnTagClickListener { _, position, _ ->
            search(tiplist[position])
//            finish()
            true
        }
        etSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                search(null)
                true
            }
            false
        }
        iv_back.setOnClickListener {
            finish()
        }
    }

    fun search(hotkey: String?) {
        when {
            !hotkey.isNullOrEmpty() -> {
                when (from) {
                    FROM_GROUP -> SearchGroupBuyResultActivity.openActivity(mContext,null, hotkey)
                    FROM_BIDDING -> SearchBiddingResultActivity.openActivity(mContext,null, hotkey)
                    FROM_LIVE -> SearchLiveResultActivity.openActivity(mContext, hotkey)
                }
            }
            etSearch.text.isNullOrEmpty() -> XToast.normal(getString(R.string.enter_search_content))
            else -> {
                when (from) {
                    FROM_GROUP -> SearchGroupBuyResultActivity.openActivity(
                        mContext,
                        null,
                        etSearch.text.toString()
                    )
                    FROM_BIDDING -> SearchBiddingResultActivity.openActivity(
                        mContext,
                        null,
                        etSearch.text.toString()
                    )
                    FROM_LIVE->{
                        SearchLiveResultActivity.openActivity(
                            mContext,
                            etSearch.text.toString()
                        )
                    }
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        Handler().postDelayed({
            KeyBoardUtils.showSoftInputFromWindow(mContext, etSearch)
        }, 300)
    }

    companion object {
        const val FROM_GROUP = 1001
        const val FROM_BIDDING = 1002
        const val FROM_LIVE = 1003
        fun openActivity(mContext: BaseActivity<*, *>, from: Int) {
            mContext.startActivity(
                bundleOf(Pair("from", from)),
                SearchGroupBuyOrBiddingOrLiveActivity::class.java
            )
        }
    }
}